package com.leal.util;

import com.leal.hive.MetaInfo;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.metastore.HiveMetaStoreClient;
import org.apache.hadoop.hive.metastore.api.FieldSchema;
import org.apache.hadoop.hive.metastore.api.MetaException;
import org.apache.thrift.TException;

import java.util.List;

/**
 * @Classname bigdata
 * @Description 获得hive表相应的元信息
 * @Date 2023/8/7 16:04
 * @Created by leal
 */
public class HiveUtil {

    public HiveMetaStoreClient getClient(String mode) {
        MetaInfo metaInfo = new MetaInfo();
        metaInfo.setHiveMetaStoreClient(mode);
        return metaInfo.getHiveMetaStoreClient();
    }


    public List<String> getTables(String mode, String dbName) throws MetaException {
        HiveMetaStoreClient client = getClient(mode);
        return client.getAllTables(dbName);
    }

    /**
     * 根据数据库表名称获得表的所有列信息
     *
     * @param mode
     * @param dbName
     * @param tableName
     * @return
     * @throws MetaException
     */
    public List<FieldSchema> getColumns(String mode, String dbName, String tableName) throws TException {
        HiveMetaStoreClient client = getClient(mode);
        return client.getSchema(dbName,tableName);
    }


    public void closeClient(HiveMetaStoreClient client) {
        client.close();
    }

}
